/**
 * 
 */
package com.jm.web.tags.table;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jm.utils.StringHelper;
import com.jm.utils.Utils;
import com.jm.web.builder.table.TableBuilder;
import com.jm.web.data.BaseDataEntity;
import com.jm.web.form.TableToolbar;
import com.jm.web.form.support.FormItem;
import com.jm.web.form.table.Table;
import com.jm.web.tags.ExportTag;
import com.jm.web.tags.support.BaseFormItemTag;

/**
 * @author yanjie
 *
 * 2011/9/22 上午10:33:20
 */
public class TableTag extends BaseFormItemTag{
	private static final long serialVersionUID = 6560389038713247961L;
	protected Table table =  null ;
	TableToolbar tableToolbar = null;
	Map<Integer,BaseDataEntity> columnsConfig = null;//导出的表头配置
	
	@Override
	protected int onStartTag() {
		this.getPage().getReturnData().newTableData();
		if(this.getPage().isExport() && !this.isEp()){
			return SKIP_BODY;
		}
		return super.onStartTag();
	}
	
	
	@Override
	protected int onEndTag() {
		this.getPage().getReturnData().removeTableData();
		return super.onEndTag();
	}
	
	@Override
	protected void preStartTag() {
		super.preStartTag();
		if(table == null){
			table = new Table();
			table.InitTag(this);
			table.init();
			table.OnSetProperty("id", StringHelper.getValue(this.getId(), "grid1"));
			if(this.getPage().isExport()){//如果是導出模式
				table.OnSetProperty("border", this.GetExtValue("border","1"));
			}else{
				table.OnSetProperty("border", "0");
			}
			
			table.OnSetProperty("border-collapse", this.GetExtValue("collapse",""));
			table.OnSetProperty("cellspacing", this.GetExtValue("cellspacing","0"));
			table.OnSetProperty("cellpadding", this.GetExtValue("cellpadding","0"));
			
			if(this.IsContainsKey("style"))
				table.OnSetProperty("style",this.GetExtValue("style",""));
			
			if(this.IsContainsKey("align"))
				table.OnSetProperty("align",this.GetExtValue("align",""));
			
			if(this.IsContainsKey("height"))
				table.OnSetProperty("height",this.GetExtValue("height",""));
			
			if(this.IsContainsKey("extAtr"))
				table.OnSetProperty("extAtr",this.GetExtValue("extAtr",""));
			
			if(this.IsContainsKey("exportconfig"))
				table.OnSetProperty("exportconfig", this.GetExtValue("exportconfig",""));
			//if(this.getPage().isNormalLayout()){
				table.setCss("listing");
				
				if("0".equals(this.GetExtValue("border","1")))
					table.setCss("listing listing-noborder");
				
				if(this.GetExtValue("width") == null)
					table.setWidth(this.GetExtValue("width", "100%"));
				else
					table.setWidth(this.GetExtValue("width", ""));
			//}	
			
			if(this.IsContainsKey("bordercolor")){
				table.setStyle(StringHelper.Format("%1$s background-color:%2$s;",table.getStyle(),this.GetExtValue("bordercolor")));
			}
			table.setView(this.GetExtValue(Table.TAG_VIEW, Table.TAG_TABLE_STYLE_NORMAL));
			
			
			//表格工具栏
			tableToolbar = this.getPage().getTableToolbars(table.getId());
			tableToolbar.InitTag(this);
			if(!StringHelper.IsNullOrEmpty(this.GetExtValue("toolbarconfig", ""))){
				BaseDataEntity toolbarConfig = BaseDataEntity.getConfig(this.GetExtValue("toolbarconfig", ""));
				if(toolbarConfig.gpbv("addRowEnab", true)){
					tableToolbar.getAddRowButton().setEnab(true);
				}else{
					tableToolbar.getAddRowButton().setEnab(false);
				}
				
				if(toolbarConfig.gpbv("deleteRowEnab", false)){
					tableToolbar.getDeleteRowButton().setEnab(true);
				}
				
				if(toolbarConfig.gpbv("saveRowEnab", false)){
					tableToolbar.getSaveRowButton().setEnab(true);
				}
			}
			if(this.GetExtValue("toolbarButtons") != null){
				tableToolbar.getItems().addAll((List<FormItem>)this.GetExtValue("toolbarButtons"));
			}
			tableToolbar.init();
		}
	}
	
	public void setView(String strTableView){
		this.SetExtValue(Table.TAG_VIEW, strTableView);
	}
	
	public void setToolbarButtons(Object strValue) {
		this.SetExtValue("toolbarButtons", strValue);
	}
	
	public void setToolbarConfig(String strValue) {
		this.SetExtValue("toolbarconfig", strValue);
	}
	
	public void setEdit(String strValue) {
		this.SetExtValue("edit", strValue);
	}
	
	public void setLoopFisrt(String strValue) {
		this.SetExtValue("loopfisrt", strValue);
	}
	
	public void setStripe(String strValue) {
		this.SetExtValue("stripe", strValue);
	}
	
	public void setAlign(String strValue) {
		this.SetExtValue("align", strValue);
	}
	
	public void setSlip(String strValue) {
		this.SetExtValue("slip", strValue);
	}
	
	public void setSelect(String strValue) {
		this.SetExtValue("select", strValue);
	}
	
	public void setCellspacing(String strValue) {
		this.SetExtValue("cellspacing", strValue);
	}
	
	public void setCellpadding(String strValue) {
		this.SetExtValue("cellpadding", strValue);
	}
	
	public void setContainer(String strValue) {
		this.SetExtValue("container", strValue);
	}
	
	public void setCustomContainer(String strValue) {
		this.SetExtValue("customcontainer", strValue);
	}
	
	public void setContainerWidth(String strValue) {
		this.SetExtValue("containerwidth", strValue);
	}
	
	public void setContainerHeight(String strValue) {
		this.SetExtValue("containerheight", strValue);
	}
	
	public void setContainerStyle(String strValue) {
		this.SetExtValue("containerstyle", strValue);
	}
	
	public void setBordercolor(String strValue) {
		this.SetExtValue("bordercolor", strValue);
	}
	
	public void setPaging(String strValue) {
		this.SetExtValue("paging", strValue);
	}
	
	public void setPagingWidth(String strValue) {
		this.SetExtValue("pagingWidth", strValue);
	}
	
	public void setPagingInfo(String strValue) {
		this.SetExtValue("pagingInfo", strValue);
	}
	
	public void setToolbar(String strValue) {
		this.SetExtValue("toolbar", strValue);
	}
	
	public void setAddRowConfig(String strValue) {
		this.SetExtValue("addrowconfig", strValue);
	}
	
	public void setDeleteRowConfig(String strValue) {
		this.SetExtValue("deleterowconfig", strValue);
	}
	
	public void setSaveRowConfig(String strValue) {
		this.SetExtValue("saverowconfig", strValue);
	}
	
	public void setJump(String strValue) {
		this.SetExtValue("jump", strValue);
	}
	
	public void setDataSet(Object obj) {
		this.SetExtValue("dataSet", obj == null ? "" : obj);
		this.getPage().setFastExport(true);
	}
	
	/* 
	 * 些绘制器只用来绘制表头与表尾 ,Tbody 由Tbody tag 实现绘制
	 */
	@Override
	public String getBuilderName() {
		return TableBuilder.class.getName();
	}
	
	@Override
	public void clear() {
		table = null ;
		tableToolbar=null;
		super.clear();
	}
	
	public Table getTable() {
		return table;
	}

	public void setTable(Table table) {
		this.table = table;
	}

	public TableToolbar getTableToolbar() {
		return tableToolbar;
	}
	
	public void setTableToolbar(TableToolbar tableToolbar) {
		this.tableToolbar = tableToolbar;
	}
	
	/**
	 * @return
	 * 返回导出的tag
	 */
	public ExportTag getExportTag(){
		return (ExportTag)this.getParent();
	}

	public Map<Integer, BaseDataEntity> getColumnsConfig() {
		if(this.columnsConfig != null)
			return this.columnsConfig;
		
		this.columnsConfig = new HashMap<Integer,BaseDataEntity>();
		return columnsConfig;
	}

	public void setColumnsConfig(Map<Integer, BaseDataEntity> columnsConfig) {
		this.columnsConfig = columnsConfig;
	}
}
